package com.bw.module_inspection.view


import android.app.Activity
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.PopupMenu
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.bw.lib_base.path.RouterPath
import com.bw.lib_base.view.BaseActivity
import com.bw.module_inspection.R
import com.bw.module_inspection.adapter.ImageAdapter
import com.bw.module_inspection.databinding.ActivityAddInspectionBinding
import com.bw.module_inspection.model.AddInspectionEntity
import com.bw.module_inspection.viewmodel.InspectionViewModel
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector

/**
 * 智慧园区-07-巡检管理V1.1-史建军-20250513
 */
@Route(path = RouterPath.PATH_INSPECTION_ADD_ACTIVITY)
class AddInspectionActivity :BaseActivity<ActivityAddInspectionBinding>() {
    //项目名称
    var project=""
    //巡检类型
    var type=""
    //巡检标准
    var standard=""
    private val viewModel by lazy { ViewModelProvider(this)[InspectionViewModel::class.java] }
    private  var imageAdapter:ImageAdapter=ImageAdapter()



    override fun initView() {
        mDataBinding.topAddLayout.ivBack.setOnClickListener {
            finish()
        }
        mDataBinding.topAddLayout.tvGoBack.text="新增点位"
        mDataBinding.topAddLayout.tvAdd.visibility=View.GONE
        mDataBinding.topAddLayout.ivMenu.visibility=View.VISIBLE
        mDataBinding.rvImg.adapter=imageAdapter
        imageAdapter.data.add("初始")
        imageAdapter.notifyDataSetChanged()

        mDataBinding.rvImg.layoutManager=GridLayoutManager(this,4)

        imageAdapter.setOnItemClickListener{adapter,view,position->
            val item=imageAdapter.data[position]
            if (item=="初始"){
                PictureSelector.create(this,103).selectPicture()

            }else{
                imageAdapter.removeAt(position)
            }

        }



        //点击项目名称弹出菜单
        mDataBinding.tvProject.setOnClickListener {
            val popupMenu =PopupMenu(this,it)
            popupMenu.menuInflater.inflate(R.menu.menu_project,popupMenu.menu)
            popupMenu.setOnMenuItemClickListener { menuItem->
                when(menuItem.itemId){
                    R.id.project1->{
                        project=menuItem.title.toString()
                        mDataBinding.tvProject.text=project
                        true
                    }
                    R.id.project2->{
                        project=menuItem.title.toString()
                        mDataBinding.tvProject.text=project
                        true
                    }
                    R.id.project3->{
                        project=menuItem.title.toString()
                        mDataBinding.tvProject.text=project
                        true
                    }
                    else ->true
                }
            }
            popupMenu.show()
        }
        //点击巡检类型弹出菜单
        mDataBinding.tvType.setOnClickListener {
            val popupMenu =PopupMenu(this,it)
            popupMenu.menuInflater.inflate(R.menu.menu_type,popupMenu.menu)
            popupMenu.setOnMenuItemClickListener { itemMenu->
                when(itemMenu.itemId){
                    R.id.type1->{
                        type=itemMenu.title.toString()
                        mDataBinding.tvType.text=type
                        true
                    }
                    R.id.type2->{
                        type=itemMenu.title.toString()
                        mDataBinding.tvType.text=type
                        true
                    }
                    R.id.type3->{
                        type=itemMenu.title.toString()
                        mDataBinding.tvType.text=type
                        true
                    }
                    R.id.type4->{
                        type=itemMenu.title.toString()
                        mDataBinding.tvType.text=type
                        true
                    }
                    else ->true
                    }

                }
            popupMenu.show()
            }

        mDataBinding.tvStandards.setOnClickListener {
            val popupMenu=PopupMenu(this,it)
            popupMenu.menuInflater.inflate(R.menu.menu_standard,popupMenu.menu)
            popupMenu.setOnMenuItemClickListener {itemMenu->
                when(itemMenu.itemId){
                    R.id.standard1->{
                        standard=itemMenu.title.toString()
                        mDataBinding.tvStandards.text=standard
                        true
                    }
                    R.id.standard2->{
                        standard=itemMenu.title.toString()
                        mDataBinding.tvStandards.text=standard
                        true

                    }
                    R.id.standard3->{
                        standard=itemMenu.title.toString()
                        mDataBinding.tvStandards.text=standard
                        true
                    }
                    R.id.standard4->{
                        standard=itemMenu.title.toString()
                        mDataBinding.tvStandards.text=standard
                        true
                    }
                    else ->true
                }
            }
            popupMenu.show()

        }
        mDataBinding.btnSend.setOnClickListener {
            val addInspection=AddInspectionEntity(
                id=1,
                patrolStandard=standard,
                patrolType = type,
                pointImages="http://images.com/1.png",
                pointLocation=mDataBinding.etPoint.text.toString(),
                pointName=mDataBinding.etName.text.toString(),
                projectId=project
            )
            viewModel.addInspection(addInspection)
            viewModel.addInspectionSuccess.observe(this){
                ToastUtils.showLong("添加点位成功")
                finish()
            }
        }
        }



    override fun getLayoutId(): Int =  R.layout.activity_add_inspection
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode==103&&resultCode==Activity.RESULT_OK){
            val pictureBean=data?.extras?.get(PictureSelector.PICTURE_RESULT)as PictureBean
            addImage(pictureBean.path)

        }
    }
    fun addImage(path:String){
        var index=imageAdapter.data.size-1
        index=if (index>=0)index else 0
        imageAdapter.data.add(index,path)
        imageAdapter.notifyDataSetChanged()
    }


}